var teamId;
var teamName;
var tmTeamPlace;
var teamGameList;
var currURL="/soccer/teamGame.jsp";
$(document).ready(function(){	
	$('#header').animate({'top':'0px'},400);
	teamId = args.teamId; //取得id的值
	teamName = args.teamName;
	//$("#teamName").text(teamName);
	
	$.getJSON(path+"/soccer/teamAction!selectActiveGameByTeam.action",{teamId: teamId}, function(data){
//	  	  debugger;
		  teamGameList = data.teamGameList;
	  	  var teamGameHtml=[];
	  	  $.each(teamGameList,function(index,obj){//debugger;
	  		  if(index%2==0){
	  			teamGameHtml.push("<div class='portfolio_item' style='background:#00CC00;margin:10px 2%;padding:10px 2%;'>");
	  			
	  		  }else{
	  			teamGameHtml.push("<div class='portfolio_item' style='background:#00CCFF;margin:10px 2%;padding:10px 2%;'>");
	  		  }
	  		  //teamGameHtml.push("<div class='service_box'>");
	  		  //teamGameHtml.push("<div class='service_content'>");
	  		  teamGameHtml.push("<h4 style='color:red'>"+moment(obj.gameDate,"YYYY-MM-DD").format("MM月DD日")+" "+obj.gameName+"</h4><h6 style='color:red'>"+GAME_STATUS_ARRY[obj.statusFlag]+"</h6>");
	  		  teamGameHtml.push("<p>时间："+moment(obj.gameDate,"YYYY-MM-DD").format("YYYY年MM月DD日")+" "+moment(obj.startTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm")+"~"+moment(obj.endTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm")+"</p>");
	  		  teamGameHtml.push("<p><strong>要求到场时间："+moment(obj.arriveTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm")+"</strong>&nbsp");
	  		  //teamGameHtml.push("<div style='font-size:30px;color:red'><a href='#' onclick='viewSignup("+obj.teamId+","+obj.rowId+")'>"+obj.bmrs+"人报名</a></div></p>");
	  		  //teamGameHtml.push("<p>地点："+obj.placeName+"</p>");
	  		  teamGameHtml.push("<div style='font-size:30px;color:red;text-align:center;position:absolute;right:20%;margin-top:-50px;margin-right:-40px;'><p href='#' onclick='viewSignup("+obj.teamId+","+obj.rowId+")'>"+obj.bmrs+"/"+obj.signupNum+"</p><p style='font-size:15px;color:red;'>人报名</p></div></p>");
	  		  teamGameHtml.push("<p>地点："+"<a href='#' onclick=\"viewPlaceBMap("+obj.teamId+","+obj.rowId+",'"+obj.placeName+"','',"+obj.placeLongitude+","+obj.placeLatitude+",1"+")\">"+obj.placeName+"</a></p>");
	  		  
	  		  teamGameHtml.push("<p>性质："+GAME_TYPE_ARRAY[obj.gameType]+"</p>");
	  		  teamGameHtml.push("<p>对手：<a href='#' onclick=\"viewOpponent("+obj.opponentTeamId+")\">"+obj.opponentName+"</a></p>");
	  		  if(obj.manageFlag==true&&(obj.statusFlag=="10"||obj.statusFlag=="20"||obj.statusFlag=="21"||obj.statusFlag=="22"||obj.statusFlag=="30")){
	  			teamGameHtml.push("<a href='#' onclick='modify("+obj.teamId+","+obj.rowId+")' class='button_12 blue'>修改</a>");
	  		  }
	  		  if(obj.statusFlag=="20"){
	  			teamGameHtml.push("<a href='#' onclick='signup("+obj.teamId+","+obj.rowId+")' class='button_12 purple' style='font-size:10px;margin:0 0 1px 1%;padding:1px 0;'>参加/不参加<br>participate or not</a>");  
	  		  }
	  		  if((obj.statusFlag=="21"||obj.statusFlag=="22")&&obj.kqFlag!="1"){
	  			teamGameHtml.push("<a href='#' onclick='kq("+obj.teamId+","+obj.rowId+","+obj.placeId+","+obj.placeLongitude+","+obj.placeLatitude+")' class='button_12 blue' style='font-size:10px;margin:0 0 1px 1%;padding:1px 0;'>签到<br>confirm arrival</a>");
	  		  }
//	  		  if(obj.statusFlag=="20"||obj.statusFlag=="21"){
//	  			teamGameHtml.push("<a href='#' class='button_12 orange'>请假</a>");
//	  		  }
	  		  if(obj.statusFlag=="30"&&obj.manageFlag==true){
	  		    teamGameHtml.push("<a href='#' onclick='recordGameData("+obj.teamId+","+obj.rowId+")' class='button_12 yellow'>记录球员数据</a>");
	  		  }
	  		  if(obj.statusFlag=="30"&&obj.scoredFlag=="1"&&obj.manageFlag==true){
		  	    teamGameHtml.push("<a href='#' onclick='assignJudge("+obj.teamId+","+obj.rowId+")' class='button_12 yellow'>记录赛果</a>");
		  	  }
	  		  if(obj.statusFlag=="30"&&obj.judge==true){
		  	    teamGameHtml.push("<a href='#' onclick='df("+obj.teamId+","+obj.rowId+")' class='button_12 yellow'>打分</a>");
		  	  }
	  		  if(obj.statusFlag=="30"&&obj.manageFlag==true){
		  	    teamGameHtml.push("<a href='#' onclick='sumGameData("+obj.teamId+","+obj.rowId+")' class='button_12 yellow'>比赛统计</a>");
		  	  }
	  		  if(obj.statusFlag=="30"||obj.statusFlag=="ZZ"){
		  	    teamGameHtml.push("<a href='#' onclick='viewGameScore("+obj.teamId+","+obj.rowId+")' class='button_12 yellow'>查看全体得分</a>");
		  	  }
	  		  if(obj.statusFlag=="30"&&obj.manageFlag==true){
		  	    teamGameHtml.push("<a href='#' onclick='closeTeamGame("+obj.teamId+","+obj.rowId+")' class='button_12 yellow'>关闭</a>");
		  	  }
	  		  teamGameHtml.push("</div>");
	  		//teamHtml.push("<tr class='info'><td>"+obj.taskDetail+"</td><td>"+obj.sendUserName+"</td><td>"+moment(obj.sendTime,"YYYY-MM-DD HH:mm:ss").format("YYYY-MM-DD HH:mm:ss")+"</td></tr>");
	  	   });
	  	   $("#teamGameList").html(teamGameHtml.join(""));  
	  		  //alert(data);
	});
	
	$("#newGameBtn").click(function(){
		var timestamp = (new Date()).valueOf();
		window.location.href=path+"/soccer/teamGameEdit.jsp?teamId="+teamId+"&timestamp="+timestamp;
	});
	
//	$("#gameHistory").click(function(){
//		var timestamp = (new Date()).valueOf();
//		window.location.href=path+"/soccer/teamGameInactive.jsp?teamId="+teamId+"&timestamp="+timestamp;
//	});
});
function goBack(){
	window.location.href=path+"/soccer/teamMain.jsp?teamId="+teamId+"&teamName="+teamName;
}
function viewGameHistory(){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/teamGameInactive.jsp?teamId="+teamId+"&timestamp="+timestamp;
}
function signup(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	var backURL="/soccer/teamGame.jsp";
	window.location.href=path+"/soccer/teamGameSignup.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&backURL="+backURL+"&timestamp="+timestamp;
}
function viewSignup(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	var backURL="/soccer/teamGame.jsp";
	window.location.href=path+"/soccer/teamGameSignupView.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&backURL="+backURL+"&timestamp="+timestamp;
}
function viewPlaceBMap(teamId,tmGameId,placeTitle,placeContent,longitude,latitude,backHandle){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/mdm/placeShow.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&placeTitle="+placeTitle+"&placeContent="+placeContent+"&longitude="+longitude+"&latitude="+latitude+"&backHandle="+backHandle+"&timestamp="+timestamp;
}
function viewOpponent(teamOpponentId){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/mdm/opponentView.jsp?teamId="+teamId+"&teamOpponentId="+teamOpponentId+"&backURL="+currURL+"&timestamp="+timestamp;
}
function kq(teamId,tmGameId,placeId,placeLongitude,placeLatitude){
	//window.location.href=path+"/soccer/teamGameKq.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&placeId="+placeId;	
	//$.ajaxSetting.async = false;
	//获取当前位置
	getLocation();
	//取球场位置
//	var param = {placeId:placeId};
//	$.getJSON(path+"/soccer/teamGameAction!getTeamPlace.action",param, function(data){
//		  tmTeamPlace = data.tmTeamPlace;
//		  txtfixlatitude.value=tmTeamPlace.placeLatitude;
//		  txtfixlongitude.value=tmTeamPlace.placeLongitude;
//	},'json');
	txtfixlatitude.value=placeLatitude;
	txtfixlongitude.value=placeLongitude;
	
	var strMessage;
	var successFlag="0";
	
	setTimeout(function(){
		//计算距离
		getDistance();
		var curDistance = parseInt(txtDistance.value);
		if(curDistance<=1000){
			successFlag="1";
			strMessage="签到成功，离球场距离"+curDistance+"米";
		}else if(curDistance>1000){
			successFlag="0";
			strMessage="签到失败！尼玛，你是在岛国签到吧。";
		}else{
			successFlag="0";
			strMessage="签到失败！计算距离错误。";
		}
		var postData={teamId:teamId,tmGameId:tmGameId,successFlag:successFlag,remark:strMessage+"(签到距离:"+curDistance+")"};
		$.post(path+"/soccer/teamGameAction!gameKq.action",postData, function(data){
			strMessage = data.strMessage;
			alert(strMessage);
		},'json');
	},2000);
	
	$('a[data-toggle="tab"]').on('shown', function (e) {debugger;
		e.target // 激活的选项卡
		e.relatedTarget // 上一个选项卡
    })
}
function df(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/teamGameTech.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&timestamp="+timestamp;
}
function modify(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/teamGameEdit.jsp?teamId="+teamId+"&tmGameId="+tmGameId+"&timestamp="+timestamp;
}
function recordGameData(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/teamGameDataEdit.jsp?teamId="+teamId+"&tmGameId="+tmGameId+"&timestamp="+timestamp;
}
function assignJudge(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+"/soccer/teamGameAssignJudge.jsp?teamId="+teamId+"&tmGameId="+tmGameId+"&timestamp="+timestamp;
}
function sumGameData(teamId,tmGameId){
	$.post(path+"/soccer/procedureAction!sumGameData.action",{tmGameId:tmGameId}, function(data){
		if(data.pRetFlag=="1"){
			alert(data.pRetMess);
			location.reload(true);
		}else{
			alert("操作失败!\n"+"错误代码:"+data.pRetCode+",错误明细:"+data.pRetDetail);
		}
	},'json');
}
function closeTeamGame(teamId,tmGameId){
	$.post(path+"/soccer/procedureAction!closeTeamGame.action",{tmGameId:tmGameId}, function(data){
		if(data.pRetFlag=="1"){
			alert(data.pRetMess);
			location.reload(true);
		}else{
			alert("操作失败!\n"+"错误代码:"+data.pRetCode+",错误明细:"+data.pRetDetail);
		}
	},'json');
}
function viewGameScore(teamId,tmGameId){
	var timestamp = (new Date()).valueOf();
	var backURL="/soccer/teamGameInactive.jsp";
	sessionStorage.setItem("teamGameDataSet-backURL", "/soccer/teamGame.jsp");
	//window.location.href=path+"/soccer/teamGameScoreView.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&backURL="+backURL+"&timestamp="+timestamp;
	window.location.href=path+"/soccer/teamGameDataSet-techScore.jsp?teamId="+teamId+"&teamName="+teamName+"&tmGameId="+tmGameId+"&backURL="+backURL+"&timestamp="+timestamp;
}
